﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using AppraisalManagement.utils;

namespace AppraisalManagement
{
    public partial class Hierarchy : System.Web.UI.MasterPage
    {
        public bool isSuccessfull = false;
        private String displayMessage = "";
        public String message 
        {
            set {
                displayMessage = "<script language = 'javascript' type='text/javascript'>alert('" + value + "') </script> ";
            }
            get
            {
                String tempMessage = displayMessage;
                displayMessage = "";
                return tempMessage;
            }
        }

        Int32 currManagerId = 0;
        List<int> nodesAdded = new List<int>();

        protected void Page_Load(object sender, EventArgs e)
        {
            TreeNode currNode = new TreeNode();

            if (Session[ApplicationConstants.NODES_ADDED.ToString()] == null)
            {
                Session[ApplicationConstants.NODES_ADDED.ToString()] = nodesAdded;
            }
            else
            {
                nodesAdded = (List<int>)Session[ApplicationConstants.NODES_ADDED.ToString()];
            }

            if (!this.IsPostBack)
            {

                Session[ApplicationConstants.SELECTED_PK.ToString()] = Session[ApplicationConstants.LOGIN_PK.ToString()];

                Int32.TryParse(Session[ApplicationConstants.LOGIN_PK.ToString()].ToString(), out currManagerId);

                HierarchyDataSetTableAdapters.HierarchyTableAdapter hierarchyAdapt = new AppraisalManagement.HierarchyDataSetTableAdapters.HierarchyTableAdapter();
                HierarchyDataSet.HierarchyDataTable hierarchyDt = new HierarchyDataSet.HierarchyDataTable();
                TreeNode innerNode = null;

                HierarchyDataSet.EmployeeDetailsRow empRow = (HierarchyDataSet.EmployeeDetailsRow)new HierarchyDataSetTableAdapters.EmployeeDetailsTableAdapter().GetData(currManagerId).Rows[0];

                currNode.Text = empRow.empName;
                currNode.Value = currManagerId.ToString();
                nodesAdded.Add(currManagerId);
                Session[AppraisalManagement.utils.ApplicationConstants.LOGIN_NAME.ToString()] = empRow.empName;


                hierarchyAdapt.Fill(hierarchyDt, currManagerId);

                foreach (HierarchyDataSet.HierarchyRow innerRow in hierarchyDt)
                {
                    innerNode = new TreeNode();
                    innerNode.Text = innerRow.empname;
                    innerNode.Value = innerRow.emp_pk.ToString();
                    currNode.ChildNodes.Add(innerNode);
                    nodesAdded.Add(innerRow.emp_pk);
                }

                hierarchyTree.Nodes.Add(currNode);
            }
        }

        //private void createHierarchy(HierarchyDataSet.HierarchyRow hierarchyRow)
        //{

        //    HierarchyDataSetTableAdapters.HierarchyTableAdapter hierarchyAdapt = new AppraisalManagement.HierarchyDataSetTableAdapters.HierarchyTableAdapter();
        //    HierarchyDataSet.HierarchyDataTable hierarchyDt = new HierarchyDataSet.HierarchyDataTable();

        //    hierarchyAdapt.Fill(hierarchyDt, hierarchyRow.emp_pk);

        //    foreach (HierarchyDataSet.HierarchyRow hRow in hierarchyDt)
        //    {
        //        currNode.Text = hierarchyRow.manager;
        //        currNode.Value = hierarchyRow.manager_pk.ToString();

        //        currManagerId = hierarchyRow.manager_pk;

        //        createHierarchy(hRow);
        //    }
        //}

        protected void hierarchyTree_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (!Int32.TryParse(Session[ApplicationConstants.LOGIN_PK.ToString()].ToString(), out currManagerId))
            {
                return;
            }

            TreeNode currNode = null;


            HierarchyDataSetTableAdapters.HierarchyTableAdapter hierarchyAdapt = new AppraisalManagement.HierarchyDataSetTableAdapters.HierarchyTableAdapter();
            HierarchyDataSet.HierarchyDataTable hierarchyDt = new HierarchyDataSet.HierarchyDataTable();
            Int32.TryParse(hierarchyTree.SelectedValue.ToString(), out currManagerId);

            Session[AppraisalManagement.utils.ApplicationConstants.SELECTED_PK.ToString()] = currManagerId;

            hierarchyAdapt.Fill(hierarchyDt, currManagerId);

            if (hierarchyDt.Rows.Count < 1)
            {
                return;
            }


            foreach (HierarchyDataSet.HierarchyRow hierarchyRow in hierarchyDt)
            {
                currNode = new TreeNode();
                if (nodesAdded.Contains(hierarchyRow.emp_pk))
                {
                    continue;
                }
                currNode.Text = hierarchyRow.empname;
                currNode.Value = hierarchyRow.emp_pk.ToString();
                nodesAdded.Add(hierarchyRow.emp_pk);
                hierarchyTree.SelectedNode.ChildNodes.Add(currNode);
            }
        }
    }
}
